﻿#region [ Code Modification History ]

//*******************************************************************************************************
//  GZipStreamToolWatch.cs 
//
//  Code Modification History:
//  -----------------------------------------------------------------------------------------------------
//  15/01/2017 - V. Dennis Kholin
//       Generated original version of source code. 
//*******************************************************************************************************

#endregion

#region [ Using ]

using System;
using System.Diagnostics;

#endregion

namespace GZipStreamTool.Base.GZipTools
{

    #region [ Enumerations ]

    /// <summary>
    ///     WatchType
    /// </summary>
    public enum WatchType
    {
        /// <summary>
        ///     Total
        /// </summary>
        Total,

        /// <summary>
        ///     Writeout
        /// </summary>
        Writeout,

        /// <summary>
        ///     Readout
        /// </summary>
        Readout,

        /// <summary>
        ///     Action
        /// </summary>
        Action,

        /// <summary>
        ///     All
        /// </summary>
        All
    }

    /// <summary>
    ///     WatchAction
    /// </summary>
    public enum WatchAction
    {
        /// <summary>
        ///     Start
        /// </summary>
        Start,

        /// <summary>
        ///     Stop
        /// </summary>
        Stop,

        /// <summary>
        ///     Reset
        /// </summary>
        Reset,

        /// <summary>
        ///     Pause
        /// </summary>
        Pause
    }

    #endregion

    /// <summary>
    ///     GZipStreamToolWatch
    /// </summary>
    public class GZipStreamToolWatch
    {
        #region [ Members ]

        private readonly Stopwatch action;
        private readonly Stopwatch readout;
        private readonly Stopwatch writeout;
        private readonly Stopwatch total;

        #endregion

        #region [ Properties ]

        /// <summary>
        ///     ActionWatch
        /// </summary>
        public string ActionWatch
        {
            get { return GenTimeSpanFromTicks(action.ElapsedTicks); }
        }

        /// <summary>
        ///     ReadoutWatch
        /// </summary>
        public string ReadoutWatch
        {
            get { return GenTimeSpanFromTicks(readout.ElapsedTicks); }
        }

        /// <summary>
        ///     WriteoutWatch
        /// </summary>
        public string WriteoutWatch
        {
            get { return GenTimeSpanFromTicks(writeout.ElapsedTicks); }
        }

        /// <summary>
        ///     TotalWatch
        /// </summary>
        public string TotalWatch
        {
            get { return GenTimeSpanFromTicks(total.ElapsedTicks); }
        }

        #endregion

        #region [ Constructors ]

        /// <summary>
        ///     GZipStreamToolWatch
        /// </summary>
        /// <param name="type"></param>
        public GZipStreamToolWatch(WatchType type)
        {
            switch (type)
            {
                case WatchType.Total:
                    total = new Stopwatch();
                    break;
                case WatchType.Readout:
                    readout = new Stopwatch();
                    break;
                case WatchType.Action:
                    action = new Stopwatch();
                    break;
                case WatchType.Writeout:
                    writeout = new Stopwatch();
                    break;
                case WatchType.All:
                    total = new Stopwatch();
                    readout = new Stopwatch();
                    action = new Stopwatch();
                    writeout = new Stopwatch();
                    break;
            }
        }

        /// <summary>
        ///     GZipStreamToolWatch
        /// </summary>
        public GZipStreamToolWatch() : this(WatchType.All)
        {
        }

        #endregion

        #region [ Methods ]

        private static string GenTimeSpanFromTicks(long ticks)
        {
            // Create a TimeSpan object and TimeSpan string from 
            // a number of ticks.
            var interval = TimeSpan.FromTicks(ticks);
            var timeInterval = interval.ToString();

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            var pIndex = timeInterval.IndexOf(':');
            pIndex = timeInterval.IndexOf('.', pIndex);
            if (pIndex < 0) timeInterval += "        ";

            if (timeInterval.Length < 12)
                timeInterval = timeInterval + ".000";
            return timeInterval.Substring(0, 12);
            // Console.WriteLine("{0,21}{1,26}", ticks, timeInterval);
        }


        /// <summary>
        ///     Action
        /// </summary>
        /// <param name="type"></param>
        /// <param name="watchaction"></param>
        public void Action(WatchType type, WatchAction watchaction)
        {
            switch (type)
            {
                case WatchType.Total:
                    switch (watchaction)
                    {
                        case WatchAction.Start:
                            total.Start();
                            break;
                        case WatchAction.Stop:
                            total.Stop();
                            break;
                        case WatchAction.Reset:
                            total.Reset();
                            break;
                        case WatchAction.Pause:
                            total.Stop();
                            break;
                    }
                    break;
                case WatchType.Readout:
                    switch (watchaction)
                    {
                        case WatchAction.Start:
                            readout.Start();
                            break;
                        case WatchAction.Stop:
                            readout.Stop();
                            break;
                        case WatchAction.Reset:
                            readout.Reset();
                            break;
                        case WatchAction.Pause:
                            readout.Stop();
                            break;
                    }
                    break;
                case WatchType.Writeout:
                    switch (watchaction)
                    {
                        case WatchAction.Start:
                            writeout.Start();
                            break;
                        case WatchAction.Stop:
                            writeout.Stop();
                            break;
                        case WatchAction.Reset:
                            writeout.Reset();
                            break;
                        case WatchAction.Pause:
                            writeout.Stop();
                            break;
                    }
                    break;
                case WatchType.Action:
                    switch (watchaction)
                    {
                        case WatchAction.Start:
                            action.Start();
                            break;
                        case WatchAction.Stop:
                            action.Stop();
                            break;
                        case WatchAction.Reset:
                            action.Reset();
                            break;
                        case WatchAction.Pause:
                            action.Stop();
                            break;
                    }
                    break;
            }
        }

        #endregion
    }
}